package com.araaya.eslimsmobile;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONObject;

import com.araaya.eslimsmobile.R;
import com.araaya.eslimsmobile.util.ElandConstant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageUpload extends Activity {
	InputStream is;
    int pic_count = 0;
    Bitmap bitmap=null;
    FileInputStream in1,in2,in3;
    BufferedInputStream buf;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

        try {
            in1 = new FileInputStream("/sdcard/eLand/1.png");
        } 
        catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            in2 = new FileInputStream("/sdcard/2.jpg");
        } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    try {
        in3 = new FileInputStream("/sdcard/3.jpg");
    } 
    catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1);
    ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
    bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1);
    byte [] imagearray1 = bao1.toByteArray();
    String ba1=Base64.encodeToString(imagearray1, Base64.DEFAULT);

    /*Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2);
    ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
    bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2);
    byte [] imagearray2 = bao2.toByteArray();
    String ba2=Base64.encode(imagearray2);

    Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3);
    ByteArrayOutputStream bao3 = new ByteArrayOutputStream();
    bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3);
    byte [] imagearray3 = bao3.toByteArray();
    String ba3=Base64.encode(imagearray3);*/

    
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
			);
    
    nameValuePairs
	.add(new BasicNameValuePair("maction","fileUpload"));
nameValuePairs.add(new BasicNameValuePair("userName", "lo"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
nameValuePairs.add(new BasicNameValuePair("landId", "2"));
    
    
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    //nameValuePairs.add(new BasicNameValuePair("image2",ba2));
    //nameValuePairs.add(new BasicNameValuePair("image3",ba3));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(ElandConstant.Mobile_URL);
        UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs);
        obj.setChunked(true);
        httppost.setEntity(obj);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        //is = entity.getContent();
        httpclient.getConnectionManager().shutdown(); 
    }
    catch(Exception e){
        //CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
        //CommonFunctions.showToast(ctx, "Unable to post captured image file: " +
        //e.toString());
    }
}
}
